package com.example.paopao.consumer.memer.service.impl;

import com.example.paopao.common.enumerator.ServiceCode;
import com.example.paopao.common.exception.ServiceException;
import com.example.paopao.consumer.memer.dao.persist.repository.IJobHiringRepository;
import com.example.paopao.consumer.memer.dao.persist.repository.WorkerIntegralRepository;
import com.example.paopao.consumer.memer.pojo.entity.UserDeductIntegral;
import com.example.paopao.consumer.memer.pojo.entity.WorkerSubssinePhoneVO;
import com.example.paopao.consumer.memer.service.WorkerIntegralService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class WorkerIntegralServiceImpl implements WorkerIntegralService {

    @Autowired
    private WorkerIntegralRepository repository;


    @Autowired
    private IJobHiringRepository iJobHiringRepository;

    @Override
    public WorkerSubssinePhoneVO getPhone(Long id) {
        return repository.getPhone(id);
    }

    @Override
    public int deductIntegral(Long userId) {
        Long aLong = iJobHiringRepository.deductIntegral(userId);
        if (aLong<=0){
            String message="账户余额不足";
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND,message);
        }
        UserDeductIntegral deductIntegral=new UserDeductIntegral();
        deductIntegral.setUserId(userId);
        deductIntegral.setBalance(Integer.valueOf(String.valueOf(aLong-1L)));
        return iJobHiringRepository.updateIntegral(deductIntegral);
    }
}
